#include "CameraApi.h"
#include <iostream>
#include "common_define.h"
 

#include <pthread.h>
#include <signal.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <malloc.h>
#include "common_define.h"


//#include <string.h>
#include <cstring>
#include <opencv2/opencv.hpp>
#ifdef __cplusplus
extern "C"
{
#include "time_fun.h"
}
#endif


using namespace cv;

#if 1
int main(){
#if 1
    CameraHandle hCamera = 0;
    tSdkFrameHead *pFrameInfo;
    BYTE *pbyBuffer;
//    BYTE **pbyBuffer;
    pbyBuffer = (BYTE *) malloc(MAX_BUFF_SIZE);
    pFrameInfo = (tSdkFrameHead *) malloc(sizeof(tSdkFrameHead));

    UINT timeout = 2000;
    struct timespec t1;
    float tdiff;
    //connect the server



    int ret = init();
#ifdef MOCK
#else
    ret = CameraInit_Hawk(hCamera);
    if(ret < 0) {

        fprintf(stderr, "[%s]- %d: %d", __FUNCTION__, __LINE__, ret);
        return ret;
    }
#endif
    while(1) {
#if 0
        double tmp=1;//0;
//        ret =CameraSetAeState_ExposureTime(hCamera,FALSE,(double)tmp);
        ret = CameraSetAeState_Mannual_ExposureTime(hCamera,tmp);
        ret = CameraSetAeState_Mannual_DGain(hCamera,tmp);
        ret = CameraSetAeState_Mannual_AGain(hCamera,tmp);
        ret = CameraSetAeState_Mannual_ISPDGain(hCamera,tmp);
#endif
        t1=tic();
        //假设先固定是3个256kb的包。
        ret = CameraGetImageBuffer(hCamera, pFrameInfo, pbyBuffer, timeout);
        if (ret < 0) {
            fprintf(stderr, "[%s]- %d: %d\n", __FUNCTION__, __LINE__, ret);
        }
        toc(t1,&tdiff);
        printf( "%s CameraGetImageBuffer %f us\n", __FUNCTION__, tdiff );

        /*    data = numpy.fromstring(self.buf, dtype='uint8')    #按uint8转换为图像矩阵
            self.image = cv2.imdecode(data, 1)                  #图像解码*/
#if 0
        cv::Mat matImage(
                cvSize(pFrameInfo->iWidth,pFrameInfo->iHeight),
                pFrameInfo->uiMediaType ==  CV_8UC3,
                pbyBuffer
        );
        imshow("from camera",matImage);
        //10ms刷新一次图片
        if(waitKey(10)==27)
        {}
#endif

    }

#endif


    return 0;
}
#endif



#if 0
int main()
{


    CameraHandle hCamera = 0;
    tSdkFrameHead *pFrameInfo;
    BYTE *pbyBuffer, *prgbBuffer;
    pFrameInfo = (tSdkFrameHead *) malloc(sizeof(tSdkFrameHead));
    pbyBuffer = (BYTE *) malloc(MAX_BUFF_SIZE);
    UINT timeout = 2000;

    BYTE pic_buff[MAX_BUFF_SIZE]={0};
    //  memcpy(send_buff,s,MAX_BUFF_SIZE);
    int fd = open(PIC_DIR_RECV, O_RDWR);
    //481,773 bytes
    int filelen = lseek(fd,0L,SEEK_END);
    read(fd,pic_buff,filelen);

    close(fd);

#if 0
    memcpy(pbyBuffer,pic_buff,filelen);
    pFrameInfo->iWidth = 1769;
    pFrameInfo->iHeight = 1060;
    CameraImageProcess(hCamera, pbyBuffer, g_pRgbBuffer,&sFrameInfo);
    cv::Mat matImage(
            cvSize(pFrameInfo->iWidth,pFrameInfo->iHeight),
            pFrameInfo->uiMediaType ==  CV_8UC3,
            prgbBuffer
    );
#endif
#if 1
    pFrameInfo->iWidth = 1769;
    pFrameInfo->iHeight = 1060;


    int H = pFrameInfo->iHeight;
    int W = pFrameInfo->iWidth;
    /*
    Mat mImage;
    mImage.create(H, W, CV_8UC3);
    memcpy(mImage.data, pic_buff, W*H*3);
    */
#if 0
    std::vector<BYTE > vec_data;
    //void insert( iterator loc, input_iterator start, input_iterator end );
    vec_data.insert(vec_data.end(), pic_buff, pic_buff+filelen);
    cv::Mat dst = cv::imdecode(vec_data, CV_LOAD_IMAGE_UNCHANGED);
#endif

    Mat matImg = Mat(H, W, CV_8UC1, pic_buff, 0);
    imshow("read from PIC_DIR", matImg);

    waitKey(-1);
#endif
}
#endif
